//
//  HBBondListProtocolCtrl.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/18.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBBondListProtocolCtrl.h"
#import "HBGetUserLoanProtocolInfo.h"
#import "HBGetUserLoanProtocolParam.h"
#import "HBGetUserLoanProtocolParser.h"

@interface HBBondListProtocolCtrl()
<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *myWebView;
@property (nonatomic, strong) NSString *webUrl;

@end

@implementation HBBondListProtocolCtrl

#pragma mark - system

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
}

- (void)buildLayout
{
    [super buildLayout];
    self.title = @"安全保障";
    self.myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    self.myWebView.delegate = self;
    [self.view addSubview:self.myWebView];
    
    self.myWebView.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
    self.myWebView.opaque = NO;
    
    if (self.isFromBondDetail)
    {
        self.title = @"合同详情";
        [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.bondDetailUrl]]];
        
    }
    else
    {
        self.title = @"服务计划协议";
        [self requestToGetUrl];
    }
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [self.myWebView stopLoading];
    self.myWebView.delegate = nil;
    [self.myWebView removeFromSuperview];
    self.myWebView = nil;
}

#pragma mark - delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    LLog(@"%@,%@",request.URL.absoluteString,self.webUrl);
    if ([request.URL.absoluteString isEqualToString:self.webUrl]||[request.URL.absoluteString isEqualToString:[NSString stringWithFormat:@"%@/",self.webUrl]])
    {
        [CustomLoadingView showHudToView:self.view];
    }
    
    if ([request.URL.absoluteString isEqualToString:self.bondDetailUrl]||[request.URL.absoluteString isEqualToString:[NSString stringWithFormat:@"%@/",self.bondDetailUrl]])
    {
        [CustomLoadingView showHudToView:self.view];
    }
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [CustomLoadingView hideHudForView:self.view];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [CustomLoadingView hideHudForView:self.view];
}

#pragma mark - net

- (void)requestToGetUrl
{
    [CustomLoadingView showHudToView:self.view];
    HBGetUserLoanProtocolParam *param = [[HBGetUserLoanProtocolParam alloc]init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    
    HBGetUserLoanProtocolParser *parser = [[HBGetUserLoanProtocolParser alloc]init];
    [NetCenter getDataFromURL:Method_getUserLoanProtocol params:param parser:parser result:^(ParserResult *myResult) {
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code==0)
        {
            HBGetUserLoanProtocolInfo *urlInfo = (HBGetUserLoanProtocolInfo *)myResult.result;
            self.webUrl = [NSString stringWithFormat:@"%@?uid=%@",urlInfo.protocolUrl,[HBLoginManager sharedHBLoginManager].currentUserInfo.uid];
            
            [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
}


@end
